package org.nlp2rdf.navigator.component.resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.nlp2rdf.navigator.client.common.ExampleDTO;
import org.nlp2rdf.navigator.client.exception.NKEQueryException;
import org.nlp2rdf.navigator.component.Component;
import org.nlp2rdf.navigator.component.exception.ComponentInitException;
import org.nlp2rdf.navigator.component.resource.maps.IMap;
import org.nlp2rdf.navigator.component.resource.maps.SimpleMap;

public class ResourceManager extends Component implements IResourceManager {
	@SuppressWarnings("unused")
	private static final Logger logger = Logger.getLogger(ResourceManager.class);

	private List<IMap> conceptCommentResource = new ArrayList<IMap>();
	private List<IMap> labelResource = new ArrayList<IMap>();
	private List<IMap> commentResource = new ArrayList<IMap>();

	@Override
	public void _init() throws ComponentInitException {
		SimpleMap im = new SimpleMap();
		Map<String, String> m = new HashMap<String, String>();
		im.setMyMap(m);
		im.setComponentConfig(getComponentConfig());
		im.init();
		m.put("http://www.w3.org/2002/07/owl#Nothing", "This is the empty class.");
		m.put("http://www.w3.org/2002/07/owl#Thing", "The class of named individuals.");
		m.put("Thing", "The class of named individuals.");
		conceptCommentResource.add(im);

		// nothing
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.nlp2rdf.navigator.component.resource.IResourceManager#getLabelsForURIs
	 * (java.util.Set)
	 */
	public Map<String, String> getLabelsForURIs(Set<String> uris) throws NKEQueryException {
		check();
		return getXXForURIs(uris, labelResource);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.nlp2rdf.navigator.component.resource.IResourceManager#
	 * getLabelsForExamples(java.util.List)
	 */
	public void getLabelsForExamples(List<ExampleDTO> examples) throws NKEQueryException {
		check();

		List<ExampleDTO> notNull = new ArrayList<ExampleDTO>();
		for (ExampleDTO ex : examples) {
			if (ex.getLabel() == null) {
				notNull.add(ex);
			}
		}

		Set<String> uris = getUris(notNull);

		Map<String, String> labels = getXXForURIs(uris, labelResource);

		for (ExampleDTO ex : notNull) {
			String label = null;
			if ((label = labels.get(ex.getUri())) != null) {
				ex.setLabel(label);
			} else {
				logger.debug("no label found for: " + ex.getUri());
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.nlp2rdf.navigator.component.resource.IResourceManager#
	 * getCommentsForExamples(java.util.List)
	 */
	public void getCommentsForExamples(List<ExampleDTO> examples) throws NKEQueryException {
		check();

		List<ExampleDTO> notNull = new ArrayList<ExampleDTO>();
		for (ExampleDTO ex : examples) {
			if (ex.getComment() == null) {
				notNull.add(ex);
			}
		}

		Set<String> uris = getUris(notNull);

		Map<String, String> labels = getXXForURIs(uris, commentResource);
		for (ExampleDTO ex : notNull) {
			String comment = null;
			if ((comment = labels.get(ex.getUri())) != null) {
				ex.setComment(comment);
			} else {
				logger.debug("no comment found for: " + ex.getUri());
			}
		}
	}

	private Set<String> getUris(List<ExampleDTO> examples) {
		Set<String> uris = new HashSet<String>();
		for (ExampleDTO ex : examples) {
			if (ex.getComment() == null) {
				uris.add(ex.getUri());
			}
		}
		return uris;
	}

	public Map<String, String> getConceptComments(Set<String> conceptNames) throws NKEQueryException {
		check();
		return getXXForURIs(conceptNames, conceptCommentResource);
	}

	private Map<String, String> getXXForURIs(Set<String> uris, List<IMap> resource) throws NKEQueryException {
		check();
		Set<String> tmp = new HashSet<String>(uris);
		Map<String, String> result = new HashMap<String, String>();
		for (IMap map : resource) {
			Map<String, String> tmpMap = map.getMap(tmp);
			logger.debug("retrieved from " + map.getClass().getSimpleName() + " : " + tmpMap.size());
			result.putAll(tmpMap);
			tmp.removeAll(result.keySet());
		}
		return result;
	}

	public void setConceptCommentResource(List<IMap> conceptCommentResource) {
		this.conceptCommentResource = conceptCommentResource;
	}

	public void setLabelResource(List<IMap> labelResource) {
		this.labelResource = labelResource;
	}

	public void setCommentResource(List<IMap> commentResource) {
		this.commentResource = commentResource;
	}

}
